package com.mrs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrs.common.ResponseResult;
import com.mrs.common.constants.SystemConstants;
import com.mrs.common.enums.AppHttpCodeEnum;
import com.mrs.common.vo.CommentVo;
import com.mrs.common.vo.PageVo;
import com.mrs.entity.Comment;
import com.mrs.exception.SystemException;
import com.mrs.mapper.CommentMapper;
import com.mrs.service.CommentService;
import com.mrs.service.UserService;
import com.mrs.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-08-09 14:37:10
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;


    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询articleId查询对应文章对应的评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();

        //查询条件: 对articleId进行判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        //根评论 rootId为-1
        queryWrapper.eq(Comment::getRootId,-1);

        //评论类型
        queryWrapper.eq(Comment::getType,commentType);

        //分页查询
        Page<Comment> page = new Page(pageNum,pageSize);
        page(page,queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());

        //查询所有根评论对应的子评论集合， 并且赋值给所对应的属性
        for (CommentVo commentVo : commentVoList) {
            //查询对应的子评论
            List<CommentVo> children=getChildren(commentVo.getId());
            //给Vo子评论成员赋值
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    /**
     * @description:  添加评论
     * @param comment
     * @return: com.mrs.common.ResponseResult
     * @author: MR.孙
     * @date: 2022/8/9 23:48
    */
    @Override
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    /**
     * @description:  根据根评论的id查询所对应的子评论的集合
     * @param id 根评论的id
     * @return: java.util.List<com.mrs.common.vo.CommentVo>
     * @author: MR.孙
     * @date: 2022/8/9 22:18
    */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        //根据根评论的id查询所对应的子评论的集合
        queryWrapper.eq(Comment::getRootId,id);

        //按照发布评论时间升序排序
        queryWrapper.orderByAsc(Comment::getCreateTime);

        List<Comment> comments = list(queryWrapper);

        List<CommentVo> commentVos = toCommentVoList(comments);
        return commentVos;
    }


    /**
     * @description:  封装CommentVoList并给toCommentUserName username赋值
     * @param list
     * @return: java.util.List<com.mrs.common.vo.CommentVo>
     * @author: MR.孙
     * @date: 2022/8/9 22:06
    */
    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);

        //遍历Vo集合
        for (CommentVo commentVo : commentVos) {
            //通过createBy查询用户的昵称并赋值
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);

            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1(根评论没有目标的userId)才进行查询
            if(commentVo.getToCommentUserId() != -1){
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }

        return commentVos;

    }

}
